package com.lb.jwitter.action;



import java.util.ArrayList;
import java.util.List;
import java.util.Map; 
 import com.opensymphony.xwork2.ActionContext; 
import com.lb.jwitter.util.ApplicationContextProvider; 

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXAHocVienChuongTrinhHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienChuongTrinhHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienMienGiam;
import com.lb.jwitter.service.TblQLDTTXAHocVienMienGiamService;
import com.lb.jwitter.service.TblQLDTTXALopMonHoc;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocService;
import com.lb.jwitter.service.TblQLDTTXAThoiKhoaBieuChinhThuc;
import com.lb.jwitter.service.TblQLDTTXAThoiKhoaBieuChinhThucService;
import com.lb.jwitter.service.TblQLDTTXAThoiKhoaBieuDuKien;
import com.lb.jwitter.service.TblQLDTTXAThoiKhoaBieuDuKienService;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKienService;
import com.lb.jwitter.service.TblQLDTTXAMonHoc;
import com.lb.jwitter.service.TblQLDTTXAMonHocService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAGiaoVien;
import com.lb.jwitter.service.TblQLDTTXAGiaoVienService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXALopMonHocDuKienUpdateAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }
    
    private String result; 
    public String getResult() { return result; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private int id; 
    public int getId() {
       return id;
 }

    public void setId(int id) {
       this.id = id;
 }

    private int monHocId; 
    public void setMonHocId(int monHocId) {
       this.monHocId = monHocId;
 }

    public int getMonHocId() {
       return monHocId;
 }

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 
    private TblQLDTTXAHocVienMienGiamService tblQLDTTXAHocVienMienGiamService;

    private int hocKyId; 
    public void setHocKyId(int hocKyId) {
       this.hocKyId = hocKyId;
 }

    public int getHocKyId() {
       return hocKyId;
 }

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private int giaoVienId; 
    public void setGiaoVienId(int giaoVienId) {
       this.giaoVienId = giaoVienId;
 }

    public int getGiaoVienId() {
       return giaoVienId;
 }

    private TblQLDTTXAGiaoVienService tblQLDTTXAGiaoVienService; 

    private String maLop; 
    public void setMaLop(String maLop) {
       this.maLop = maLop;
 }

    public String getMaLop() {
       return maLop;
 }
    private String ghiChu; 
    public void setGhiChu(String ghiChu) {
       this.ghiChu = ghiChu;
 }

    public String getGhiChu() {
       return ghiChu;
 }

    private TblQLDTTXAThoiKhoaBieuDuKienService tblQLDTTXAThoiKhoaBieuDuKienService;
    private TblQLDTTXAThoiKhoaBieuChinhThucService tblQLDTTXAThoiKhoaBieuChinhThucService;

    private String tblQLDTTXAHocVienDangKyMonHocStr;

    private TblQLDTTXALopMonHocDuKien tblQLDTTXALopMonHocDuKien; 
    public void setTblQLDTTXALopMonHocDuKien (TblQLDTTXALopMonHocDuKien tblQLDTTXALopMonHocDuKien) {
        this.tblQLDTTXALopMonHocDuKien = tblQLDTTXALopMonHocDuKien;
 }

    private TblQLDTTXALopMonHocDuKienService tblQLDTTXALopMonHocDuKienService; 
    private TblQLDTTXALopMonHocService tblQLDTTXALopMonHocService;
    private TblQLDTTXALopMonHocHocVienService tblQLDTTXALopMonHocHocVienService;

    private TblQLDTTXAHocVienDangKyMonHocService tblQLDTTXAHocVienDangKyMonHocService;
    private TblQLDTTXAHocVienChuongTrinhHocService tblQLDTTXAHocVienChuongTrinhHocService;
    
	private int preUpdate(Message msg, TblQLDTTXALopMonHocDuKien obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblQLDTTXALopMonHocDuKien obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LopMonHoc_DuKien", "sua")){ 
        return "noprivilege"; 
    } 
        tblQLDTTXALopMonHocDuKienService = (TblQLDTTXALopMonHocDuKienService) context.getBean("tbl_QLDTTXA_LopMonHoc_DuKienClient"); 
        tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 
        tblQLDTTXALopMonHocService = (TblQLDTTXALopMonHocService) context.getBean("tbl_QLDTTXA_LopMonHocClient"); 
        tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService) context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient"); 
        tblQLDTTXAHocVienDangKyMonHocService = (TblQLDTTXAHocVienDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_DangKyMonHocClient"); 
        
        tblQLDTTXAGiaoVienService = (TblQLDTTXAGiaoVienService) context.getBean("tbl_QLDTTXA_GiaoVienClient"); 
        
        tblQLDTTXAThoiKhoaBieuDuKienService = (TblQLDTTXAThoiKhoaBieuDuKienService) context.getBean("tbl_QLDTTXA_ThoiKhoaBieuDuKienClient"); 
        tblQLDTTXAThoiKhoaBieuChinhThucService = (TblQLDTTXAThoiKhoaBieuChinhThucService) context.getBean("tbl_QLDTTXA_ThoiKhoaBieuChinhThucClient");
        tblQLDTTXAHocVienMienGiamService = (TblQLDTTXAHocVienMienGiamService) context.getBean("tbl_QLDTTXA_HocVien_MienGiamClient");
        tblQLDTTXAHocVienChuongTrinhHocService = (TblQLDTTXAHocVienChuongTrinhHocService)  context.getBean("tbl_QLDTTXA_HocVien_ChuongTrinhHocClient");
        
        
        msg = new Message(); 
        
        /*******************************************
         * cap nhat lop mon hoc du kien da duoc mo
         ************************************************/
        tblQLDTTXALopMonHocDuKien = tblQLDTTXALopMonHocDuKienService.gettblQLDTTXALopMonHocDuKien(new Long(id));
        
        if (tblQLDTTXALopMonHocDuKien.isDuocMo()){
        	result = "1";
	    	return Action.SUCCESS;
        }
        
        tblQLDTTXALopMonHocDuKien.setMaLop(maLop);
        
        tblQLDTTXALopMonHocDuKien.setGhiChu(ghiChu); 

        tblQLDTTXALopMonHocDuKien.setDuocMo(true); 

        preUpdate(msg,tblQLDTTXALopMonHocDuKien); 
       if (msg.isPreMessage_b()) 
       { 
           tblQLDTTXALopMonHocDuKienService.updatetblQLDTTXALopMonHocDuKien(tblQLDTTXALopMonHocDuKien); 
           postUpdate(msg, tblQLDTTXALopMonHocDuKien); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       
       /**************************************
        * Tao lop mon hoc chinh thuc
        **************************************/

       TblQLDTTXALopMonHoc tblQLDTTXALopMonHoc = new TblQLDTTXALopMonHoc();
       tblQLDTTXALopMonHoc.setMaLop(maLop);
       tblQLDTTXALopMonHoc.setGhiChu(ghiChu);
       tblQLDTTXALopMonHoc.setGiaoVienId(tblQLDTTXALopMonHocDuKien.getGiaoVienId());
       tblQLDTTXALopMonHoc.setHocKyId(tblQLDTTXALopMonHocDuKien.getHocKyId());
       tblQLDTTXALopMonHoc.setMonHocId(tblQLDTTXALopMonHocDuKien.getMonHocId());
       
       Long id = tblQLDTTXALopMonHocService.createtblQLDTTXALopMonHoc(tblQLDTTXALopMonHoc);
       tblQLDTTXALopMonHoc.setId(id);
       /*
        * Chuyen danh sach hoc vien cho lop mon hoc chinh thuc
        * Cap nhat phieu dang ky trang thai da duoc xep lop
        */
       List<TblQLDTTXALopMonHocHocVien> hocViens = new ArrayList<TblQLDTTXALopMonHocHocVien>();
       JSONArray jsonTblQLDTTXAHocVienDangKyMonHoc = (JSONArray)JSONSerializer.toJSON(tblQLDTTXAHocVienDangKyMonHocStr); 
       for (int i = 0; i < jsonTblQLDTTXAHocVienDangKyMonHoc.size(); i++) 
        { 
    	   
    	   JSONObject jsObj = jsonTblQLDTTXAHocVienDangKyMonHoc.getJSONObject(i);             
            
            TblQLDTTXAHocVienDangKyMonHoc tblQLDTTXAHocVienDangKyMonHoc 
            		= tblQLDTTXAHocVienDangKyMonHocService
	        		   	.gettblQLDTTXAHocVienDangKyMonHoc(
	        		   			new Long(jsObj.getInt("tblQLDTTXAHocVienDangKyMonHocId"))
	        		   	);
            // cap nhat  trang thai phieu dang ky la da xep lop
            tblQLDTTXAHocVienDangKyMonHoc.setDaXepLop(true);
            tblQLDTTXAHocVienDangKyMonHocService.updatetblQLDTTXAHocVienDangKyMonHoc(tblQLDTTXAHocVienDangKyMonHoc);
            // Kiem tra mien mon hoc, neu mon hoc duoc mien thi khong dua vao danh sach lop
            
            List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
            if(true){
     	       	ConditionReport con = new ConditionReport(); 
     	           con.setCol("donMGId.hocVienId.id");
     	           con.setValue1(tblQLDTTXAHocVienDangKyMonHoc.getPhieuDangKyId().getHocVienId().getId());
     	           con.setOperator("eq");
     	           cons.add(con);		 
     	   	}
     			if(true){
     	       	ConditionReport con = new ConditionReport(); 
     	           con.setCol("donMGId.trangThaiId");
     	           con.setValue1(2);
     	           con.setOperator("eq");
     	           cons.add(con);		 
     	   	}
     			if(true){
     	       	ConditionReport con = new ConditionReport(); 
     	           con.setCol("duocMienGiam");
     	           con.setValue1(true);
     	           con.setOperator("eq");
     	           cons.add(con);		 
     	   	}
     			if(true){
         	       	ConditionReport con = new ConditionReport(); 
         	           con.setCol("monHocId.id");
         	           con.setValue1(tblQLDTTXALopMonHoc.getMonHocId().getId());
         	           con.setOperator("eq");
         	           cons.add(con);		 
         	   	}
            int tblHocVienMienGiams = tblQLDTTXAHocVienMienGiamService.counttblQLDTTXAHocVienMienGiamByCondition(cons);
            
            
            
            TblQLDTTXALopMonHocHocVien obj0 = new TblQLDTTXALopMonHocHocVien(); 
          obj0.setHocVienId(tblQLDTTXAHocVienDangKyMonHoc.getPhieuDangKyId().getHocVienId()); 
          obj0.setLopMonHocId(tblQLDTTXALopMonHoc); 
          obj0.setDiemCK(0.0);
          obj0.setDiemGK(0.0);
          obj0.setDiemTH(0.0);
          obj0.setTongdiem(0.0);
          		
          if (tblHocVienMienGiams > 0){
        	  obj0.setTrangThai(2); // trang thai: mien mon hoc
        	  
        	  // cat nhat chuong trinh hoc
        	  cons.clear();
	       	   { 
	       	       ConditionReport con = new ConditionReport(); 
	       	       con.setCol("hocVienId.id");
	       	       con.setValue1(tblQLDTTXAHocVienDangKyMonHoc.getPhieuDangKyId().getHocVienId().getId());
	       	       con.setOperator("eq");
	       	       cons.add(con);	
	       	   }
	       	   { 
	       	       ConditionReport con = new ConditionReport(); 
	       	       con.setCol("monHocId.id");
	       	       con.setValue1(tblQLDTTXALopMonHoc.getMonHocId().getId());
	       	       con.setOperator("eq");
	       	       cons.add(con);	
	       	   }
	       	List<TblQLDTTXAHocVienChuongTrinhHoc> tblQLDTTXAHocVienChuongTrinhHocs = tblQLDTTXAHocVienChuongTrinhHocService.findtblQLDTTXAHocVienChuongTrinhHocByCondition(cons, 0, 0);
	       	   
	       	for(TblQLDTTXAHocVienChuongTrinhHoc tblQLDTTXAHocVienChuongTrinhHoc : tblQLDTTXAHocVienChuongTrinhHocs) {
		       		
       		   tblQLDTTXAHocVienChuongTrinhHoc.setHoanThanh( true );

       		   tblQLDTTXAHocVienChuongTrinhHocService.updatetblQLDTTXAHocVienChuongTrinhHoc(tblQLDTTXAHocVienChuongTrinhHoc);	            	   
	              	       	   
	       	}
        	  
          } else {
        	  obj0.setTrangThai(0); // trang thai: dang hoc
          }
           
          tblQLDTTXALopMonHocHocVienService.createtblQLDTTXALopMonHocHocVien(obj0);
          hocViens.add(obj0);
          
          
      } 
       

       /*
        * Chuyen thoi khoa bieu du kien sang thoi khoa bieu chinh thuc
        */
       
       //Lay thoi khoa bieu Du kien cua lopMonHocDuKien
       List<ConditionReport> cons = new ArrayList<ConditionReport> ();
  		if(tblQLDTTXALopMonHocDuKien != null){
  			ConditionReport con = new ConditionReport();
  			con.setCol("lopMonHocDuKienId.id");
  			con.setValue1(tblQLDTTXALopMonHocDuKien.getId());
  			con.setOperator("eq");
  			cons.add(con);
  		}else {
  			  return Action.ERROR;
  		}
  		if(tblQLDTTXALopMonHocDuKien.getHocKyId() != null){
  			ConditionReport con = new ConditionReport();
  			con.setCol("hocKyId.id");
  			con.setValue1(tblQLDTTXALopMonHocDuKien.getHocKyId().getId());
  			con.setOperator("eq");
  			cons.add(con);
  		}else {
  			  return Action.ERROR;
  		}
  		List<TblQLDTTXAThoiKhoaBieuDuKien> tblTKBDuKiens = tblQLDTTXAThoiKhoaBieuDuKienService.findtblQLDTTXAThoiKhoaBieuDuKienByCondition(cons, 0, 0);
  		
  		//chuyen TKB Du kien sang TKB ChinhThuc
  		if(tblTKBDuKiens != null || tblTKBDuKiens.size() > 0){
  			// kiem tra trung TKB hoc vien
  			List<TblQLDTTXAThoiKhoaBieuChinhThuc> tblQLDTTXAThoiKhoaBieuChinhThucs = new ArrayList<TblQLDTTXAThoiKhoaBieuChinhThuc>();
  			 for(TblQLDTTXAThoiKhoaBieuDuKien tblTKBDuKien : tblTKBDuKiens){
				 TblQLDTTXAThoiKhoaBieuChinhThuc tblQLDTTXAThoiKhoaBieuChinhThuc = new TblQLDTTXAThoiKhoaBieuChinhThuc();
				 tblQLDTTXAThoiKhoaBieuChinhThuc.setHocKyId(tblTKBDuKien.getHocKyId());
				 tblQLDTTXAThoiKhoaBieuChinhThuc.setLopMonHocId(tblQLDTTXALopMonHoc);
				 tblQLDTTXAThoiKhoaBieuChinhThuc.setNgayId(tblTKBDuKien.getNgayId());
				 tblQLDTTXAThoiKhoaBieuChinhThuc.setPhongId(tblTKBDuKien.getPhongId());
				 tblQLDTTXAThoiKhoaBieuChinhThuc.setTietHocId(tblTKBDuKien.getTietHocId());
  				 
  				cons.clear();
  				ConditionReport con1 = new ConditionReport();
  		    	con1.setCol("hocKyId.id");
  		    	con1.setValue1(tblQLDTTXAThoiKhoaBieuChinhThuc.getHocKyId().getId());
  		    	con1.setOperator("eq");
  		    	cons.add(con1);
  		    	
  		    	ConditionReport con2 = new ConditionReport();
  		    	con2.setCol("ngayId.id");
  		    	con2.setValue1(tblQLDTTXAThoiKhoaBieuChinhThuc.getNgayId().getId());
  		    	con2.setOperator("eq");
  		    	cons.add(con2);
  		    	
  		    	ConditionReport con3 = new ConditionReport();
  		    	con3.setCol("tietHocId.id");
  		    	con3.setValue1(tblQLDTTXAThoiKhoaBieuChinhThuc.getTietHocId().getId());
  		    	con3.setOperator("eq");
  		    	cons.add(con3);
  				 
  		    	List<TblQLDTTXAThoiKhoaBieuChinhThuc> tkbChinhThucs = tblQLDTTXAThoiKhoaBieuChinhThucService.findtblQLDTTXAThoiKhoaBieuChinhThucByCondition(cons, 0, 0);
  		    	for(TblQLDTTXAThoiKhoaBieuChinhThuc tkb : tkbChinhThucs){
  		    		for(TblQLDTTXALopMonHocHocVien hocVien : hocViens){
  		    			cons.clear();
  		    			ConditionReport con4 = new ConditionReport();
  		  		    	con4.setCol("lopMonHocId.id");
  		  		    	con4.setValue1(tkb.getLopMonHocId().getId());
  		  		    	con4.setOperator("eq");
  		  		    	cons.add(con4);
  		  		    	
  		  		    	ConditionReport con5 = new ConditionReport();
		  		    	con5.setCol("hocVienId.id");
		  		    	con5.setValue1(hocVien.getHocVienId().getId());
		  		    	con5.setOperator("eq");
		  		    	cons.add(con5);
		  		    	
		  		    	int num = tblQLDTTXALopMonHocHocVienService.counttblQLDTTXALopMonHocHocVienByCondition(cons);
		  		    	if(num > 0){
		  		    		//bao loi trung TKB hoc vien
		  		    		result = "0";
		  		    		return Action.SUCCESS;
		  		    	}
  		    		}
  		    	}
  				tblQLDTTXAThoiKhoaBieuChinhThucs.add(tblQLDTTXAThoiKhoaBieuChinhThuc);
  			}//end-for tkbDuKiens
  			 
  			 for(TblQLDTTXAThoiKhoaBieuChinhThuc tblTKBChinhThuc : tblQLDTTXAThoiKhoaBieuChinhThucs){
  				tblQLDTTXAThoiKhoaBieuChinhThucService.createtblQLDTTXAThoiKhoaBieuChinhThuc(tblTKBChinhThuc);
  				
  			 }
  				 
  		}  // end-if tkbDuKiens != null	


        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_QLDTTXA_LopMonHoc_DuKien Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }

public String getTblQLDTTXAHocVienDangKyMonHocStr() {
	return tblQLDTTXAHocVienDangKyMonHocStr;
}

public void setTblQLDTTXAHocVienDangKyMonHocStr(
		String tblQLDTTXAHocVienDangKyMonHocStr) {
	this.tblQLDTTXAHocVienDangKyMonHocStr = tblQLDTTXAHocVienDangKyMonHocStr;
} 
 } 
 
